package com.commerce.service.impl;

import com.commerce.common.Reminder;
import com.commerce.mapping.RoleMapping;
import com.commerce.model.Role;
import com.commerce.page.Page;
import com.commerce.page.RolePage;
import com.commerce.service.RoleService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author wyj
 * @Title:
 * @Description:
 */
@Service
public class RoleServiceImpl implements RoleService{

    @Autowired
    private RoleMapping roleMapping;
    /**
     * 新增
     *
     * @param role
     * @return
     */
    @Override
    public String insert(Role role, HttpServletRequest request) {
        if(roleMapping.selectByName(role.getName())>0)
            return Reminder.rolenamerepetitionerror.name();
        if(roleMapping.insert(role)>0)
            return Reminder.success.getName();
        return Reminder.saveerror.getName();
    }

    /**
     * 修改
     *
     * @param role
     * @return
     */
    @Override
    public String update(Role role, HttpServletRequest request) {
        if(roleMapping.selectByName(role.getName())>0)
            return Reminder.rolenamerepetitionerror.getName();
        if(roleMapping.update(role)>0)
            return Reminder.success.getName();
        return Reminder.updateerror.getName();
    }

    /**
     * 根据ID删除
     *
     * @param id
     * @return
     */
    @Override
    public String deleteById(int id) {
        if(roleMapping.deleteById(id)>0)
            return Reminder.success.getName();
        return Reminder.deleteerror.getName();
    }

    /**
     * 分页查询
     *
     * @param page
     * @return
     */
    @Override
    public Page selectByPage(RolePage page) {
        PageHelper.startPage(page.getPage(),page.getLimit());
        page.setList(roleMapping.selectPage(page));
        return page;
    }

    /**
     * 根据ID查询
     *
     * @param id
     * @return
     */
    @Override
    public Role selectById(int id) {
        return roleMapping.selectById(id);
    }

    /**
     * 根据多个ID查询
     *
     * @param ids
     * @return
     */
    @Override
    public List<Role> selectByRoleIds(String ids) {
        return roleMapping.selectByRoleIds(ids);
    }
}
